import iButton from '@/components/carGoods/i-button.vue'

export default {
  components: { iButton },
  data(){
    return {
      addressShow: false,  // 收显示货地址列表弹框
      radioAddress: NaN,  // 控制单选收货地址
      addressList: [], // 收货地址列表
      addressData: {},  // 选择用于显示的地址
      storeName: '',  // 当前用户店铺名称
      car_Part_Offer_Details_Ids: null,  // 配件信息
      detailData: {},  // 订单
    }
  },

  onLoad(e){
    // 配件信息
    this.car_Part_Offer_Details_Ids = JSON.parse(e.data)
  },

  created() {
    this.getInfo()
    this.getAddressList()
  },

  methods: {
    // 获取收货地址
    async getAddressList() {
      uni.showLoading()
      try {
        let data = {
          'PageIndex': 1,
          'PageSize': 100,
        }
        const res = await this.$api.common.addressList(data)
        this.addressList = res.data.list.map(item => ({ ...item, addressAll: item.province+item.city+item.district+item.address }))
        if(isNaN(this.radioAddress)){
          this.addressList.forEach( (item,i) => {
            if(item.id === this.addressData.id){
              this.radioAddress = i
            }
          })
        }
      } catch (e) {
        console.log(e)
      }
      uni.hideLoading()
    },

    //获取订单预览信息
    async getInfo(){
      const res = await this.$api.accessory.previewOrder({
      	car_Part_Offer_Details_Ids: this.car_Part_Offer_Details_Ids
      })
      this.detailData = res.data
      this.addressData = {
        addressAll: res.data.list[0].buy_address,
        id: res.data.list[0].receiver_Address_Id,
        mobile: res.data.list[0].buy_phone,
        name: res.data.list[0].buy_name,
      }
    },

    // 单选地址。选中地址
    checkedAddress(index) {
      this.radioAddress = index
      this.addressData = this.addressList[index]
      this.addressShow = false
    },

    // 跳转新增/编辑地址页面
    goAddressPage(data, type) {
      let urls = {
        'add': '/pages_common/newAddress/newAddress',
        'edit': `/pages_common/editAddress/editAddress?addressid=${JSON.stringify(data)}`
      }
      uni.navigateTo({
        url: urls[type]
      })
    },

    // 提交订单
    submitOrder() {
      this.$api.accessory.createOrder({
        car_Part_Offer_Details_Ids: this.car_Part_Offer_Details_Ids,
        receiver_Address_Id: this.addressData.id
      }).then(res =>{
        uni.showToast({ title: '提交成功' })
        console.log('提交成功，待付款', res)
        uni.redirectTo({
          url: '/pages_order/pay/pay?orderNo=' + res.data.orderPartsid + '&price=' + this.detailData.total_money + '&type=accessory'
        })
      }).catch (err => {
        console.log(err)
      })
    },

    handleImg(imgStr){
      console.log(imgStr)
      if(imgStr){
        return JSON.parse(imgStr)[0]
      }else {
        return ''
      }

    }

  }
}